import multiprocessing
import time

N0 = 1000_00
N = N0
TASKS = 10


class Environment:
    def __init__(self) -> None:
        self.increment = 1


parent = Environment()


class Agent:
    def __init__(self) -> None:
        self.parent = parent

    def calc(self, n):
        i = 0
        while i < n:
            i += parent.increment
        return


def test_single_proc(n):
    t0 = time.time()
    for i in range(TASKS):
        a = Agent()
        a.calc(n)
    t1 = time.time()
    print("单进程", t1-t0)


def func_for_each( n):
    a = Agent()
    a.calc(n)
    return n


def test_multi_proc(n):
    t0 = time.time()

    with multiprocessing.Pool(processes=4) as pool:
        values = pool.map(func_for_each, [n for i in range(TASKS)])

    t1 = time.time()
    print("多进程", t1-t0)


if __name__ == "__main__":
    for index in range(3):
        n = N0*10**index
        print("===============n=", n, "=================")
        test_single_proc(n)
        test_multi_proc(n)
